#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <list>
using namespace std;

void test_string1()
{
	string s1;
	//string s2("hello world!!!");
	// string (const char* s);
	string s2 = "hello world!!!";
	cout << s1 << endl;
	cout << s2 << endl;

	string s3(s2);
	cout << s3 << endl;

	string s4(s2, 6, 5);
	cout << s4 << endl;

	// 第三个参数len大于后面字符长度，有多少拷贝多少拷贝到结尾
	string s5(s2, 6, 15);
	cout << s5 << endl;
	string s6(s2, 6);
	cout << s6 << endl;

	string s7("hello world", 5);
	cout << s7 << endl;

	string s8(100, 'x');
	cout << s8 << endl;

	//cin >> s1 >> s2;
	//cout << s1 << endl;
	//cout << s2 << endl;
}

void test_string2()
{
	string s1;
	string s2 = "hello world!!!"; // 构造+拷贝构造 -》优化 -- 直接构造

	s1 = s2;
	s1 = "xxxx"; // =
	s1 = 'y';
}

void test_string3()
{
	string s1("hello world");
	cout << s1[0] << endl;
	s1[0] = 'x';
	cout << s1[0] << endl;
	cout << s1 << endl;

	// 要求遍历string，每个字符+1
	for (size_t i = 0; i < s1.size(); ++i)
		//for (size_t i = 0; i < s1.length(); ++i)
	{
		s1[i]++;
	}
	cout << s1 << endl;

	const string s2("world");
	for (size_t i = 0; i < s2.size(); ++i)
	{
		//s2[i]++;
		cout << s2[i] << " ";
	}
	cout << endl;
	cout << s2 << endl;

	//s2[6];  内部会检查越界
}

void test_string4()
{
	string s("hello");
	string::iterator it = s.begin();
	while (it != s.end())
	{
		(*it)++;
		cout << *it << " ";
		++it;
	}
	cout << endl;

	// 范围for -- 自动迭代，自动判断结束
	// 依次取s中每个字符，赋值给ch
	/*for (auto ch : s)
	{
		ch++;
		cout << ch << " ";
	}*/
	for (auto& ch : s)
	{
		ch++;
		cout << ch << " ";
	}
	cout << endl;

	cout << s << endl;

	list<int> lt(10, 1);
	list<int>::iterator lit = lt.begin();
	while (lit != lt.end())
	{
		cout << *lit << " ";
		++lit;
	}
	cout << endl;

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	// 范围for底层其实就是迭代器
}

void PrintString(const string& str)
{
	//string::const_iterator it = str.begin();
	auto it = str.begin();
	while (it != str.end())
	{
		//*it = 'x';
		cout << *it << " ";
		++it;
	}
	cout << endl;

	//auto rit = str.begin();
	string::const_reverse_iterator rit = str.rbegin();
	while (rit != str.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;
}

// iterator/const_iterator
// reverse_iterator/const_reverse_iterator

void test_string5()
{
	string s("hello");
	string::reverse_iterator rit = s.rbegin();
	while (rit != s.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;

	PrintString(s);
}

// 17：10继续
void test_string6()
{
	string s("hello");
	s.push_back('-');
	s.push_back('-');
	s.append("world");
	cout << s << endl;

	string str("我来了");
	s += '@';
	s += str;
	s += "!!!";
	cout << s << endl;

	s.append(++str.begin(), --str.end());
	cout << s << endl;

	//string copy(++s.begin(), --s.end());
	string copy(s.begin() + 5, s.end() - 5);
	cout << copy << endl;
}

void test_string7()
{
	/*string s1;
	string s2("11111111111111");
	cout << s1.max_size() << endl;
	cout << s2.max_size() << endl;

	cout << s2.capacity() << endl;
	cout << s2.capacity() << endl;*/

	string s;
	// reverse 逆置
	s.reserve(1000); //保留 开空间
	//s.resize(1000, 'x');    //     开空间 + 初始化
	size_t sz = s.capacity();
	sz = s.capacity();
	cout << "capacity changed: " << sz << '\n';
	cout << "making s grow:\n";
	for (int i = 0; i < 1000; ++i)
	{
		s.push_back('c');
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed: " << sz << '\n';
		}
	}
}

int main()
{
	test_string7();

	return 0;
}
